package com.infonautics.dao.imp;


import java.util.List;

import com.hibernate.Assignpurview;
import com.hibernate.HibernateDao;
import com.hibernate.Userrole;
import com.infonautics.dao.IUserroleDao;
import com.infonautics.page.PageBean;
import com.system.dao.ISystemDao;

public class IUserroleDaoImp implements ISystemDao<Userrole>,IUserroleDao{

	HibernateDao<Userrole> userroleDao = new HibernateDao<Userrole>();
	HibernateDao<Assignpurview> assignDao = new HibernateDao<Assignpurview>();
	
	@Override
	public PageBean<Userrole> doList(int pageSize, int page,
			int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void doAdd(Userrole object) throws Exception {
		if(object != null){
			userroleDao.addObject(object);
		}
	}

	@Override
	public void doModfiy(Userrole object) throws Exception {
		if(object != null){
			userroleDao.updateObject(object);
		}
	}

	@Override
	public void doDelete(int id) throws Exception {
		userroleDao.deleteObject(id, Userrole.class);
		
	}

	@Override
	public Userrole doOne(int id) throws Exception {
		
		return userroleDao.getObject(id, Userrole.class);
	}

	@Override
	public PageBean<Userrole> doUserroleList(int pageSize, int page)  throws Exception{
		final String hql = "from Userrole order by id desc";
		int allRow = userroleDao.getAllRowCount("select count(*) from Userrole"); // 
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Userrole> list = userroleDao.getObjects(hql, offset,length);
	
		PageBean<Userrole> pageBean = new PageBean<Userrole>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public Object getValue(String sql)  throws Exception{
		
		return userroleDao.getValue(sql);
	}

	@Override
	public void doDeletePurview(int roleid)  throws Exception{
		assignDao.ExecuteHQL("Delete from Assignpurview a where a.userrole.id="+roleid);
		
	}

	@Override
	public void assignPurview(Assignpurview purview) {
		if(purview != null){
			assignDao.addObject(purview);
		}
		
	}

	@Override
	public List<Assignpurview> doAssignList(int roleid) throws Exception {
		String hql = "from Assignpurview where userrole.id="+roleid;
		List<Assignpurview> assignList = assignDao.getListObjects(hql);
		return assignList;
	}

}
